package services

import (
	"context"

	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"go.mongodb.org/mongo-driver/mongo"
)

// SavePictureToMongoDB 保存图片到 MongoDB
func SavePictureToMongoDB(cli *mongo.Client, filename string, fileBytes []byte, x, y, w, h int) (string, error) {
	coll := cli.Database("testdb").Collection("pictures")

	// 创建文档
	pictureDoc := bson.M{
		"filename": filename,
		"content":  fileBytes,
		"startX":   x,
		"startY":   y,
		"width":    w,
		"height":   h,
	}

	// 插入文档
	result, err := coll.InsertOne(context.TODO(), pictureDoc)
	if err != nil {
		return "", err
	}

	return result.InsertedID.(primitive.ObjectID).Hex(), nil
}
